package com.java1412.service.impl;

import com.java1412.dto.PayInfoDto;
import com.java1412.entity.PayInfo;
import com.java1412.entity.User;
import com.java1412.mapper.PayInfoMapper;
import com.java1412.mapper.UserMapper;
import com.java1412.service.PayInfoService;
import com.java1412.utils.ResponseUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author java1412
 * @since 2023-06-27 09:46:50
 */
@Service
@Transactional
public class PayInfoServiceImpl implements PayInfoService {
    @Resource
    private PayInfoMapper payInfoMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public ResponseUtil add(PayInfo payInfo) {
        payInfo.setCreateTime(new Date());
        payInfo.setUpdateTime(new Date());
        payInfoMapper.add(payInfo);
        return ResponseUtil.ok("添加支付成功");
    }

    @Override
    public ResponseUtil findAll() {
        List<PayInfo> payInfoList = payInfoMapper.findAll();
        List<PayInfoDto> payInfoDtoList = new ArrayList<>();
        for (PayInfo payInfo : payInfoList) {
            // 获取用户编号
            Integer userId = payInfo.getUserId();
            // 获取人的信息
            User user = userMapper.findById(userId);
            user.setPassword(null);
            // 将user添加到payInfoDto,将payInfoDto加载payInfoDtoList
            PayInfoDto payInfoDto = new PayInfoDto();
            BeanUtils.copyProperties(payInfo, payInfoDto);
            payInfoDto.setUser(user);
            payInfoDtoList.add(payInfoDto);
        }
        return ResponseUtil.ok("获取支付信息成功", payInfoDtoList);
    }
}
